/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgMetodoPyramid.pkgPresentacion;

import java.awt.Dialog;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.sf.jasperreports.engine.JRException;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import pkgMetodoPyramid.pkgReporte.GestorGraficos;
import pkgMetodoPyramid.pkgReporte.ProcessReportException;
import pkgMetodoPyramid.pkgSoporte.Constants;
import pkgModuloPrincipal.pkgFachadaPrincipal.pkgSoporte.ConstantsModuloPpal;
import pkgUtil.UtilLayoutPantalla;

/**
 *
 * @author Administrador
 */
public class PllaGraficosPyramid extends javax.swing.JFrame {

    private GestorGraficos gestor;

    /**
     * Creates new form PllaGraficosPyramid
     */
    public PllaGraficosPyramid(GestorGraficos gestor) {
        initComponents();
        this.gestor = gestor;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGRpt = new javax.swing.ButtonGroup();
        jTPGraficos = new javax.swing.JTabbedPane();
        jPGraph1 = new javax.swing.JPanel();
        jspGraf1Mujeres = new javax.swing.JScrollPane();
        jspGraf1Varones = new javax.swing.JScrollPane();
        jPGraph2 = new javax.swing.JPanel();
        jspGraf2Mujeres = new javax.swing.JScrollPane();
        jspGraf2Varones = new javax.swing.JScrollPane();
        jPGraph3 = new javax.swing.JPanel();
        jspGraf3Mujeres = new javax.swing.JScrollPane();
        jspGraf3Varones = new javax.swing.JScrollPane();
        jPDatos = new javax.swing.JPanel();
        lblTotalVarones = new javax.swing.JLabel();
        lblValueTotalVarones = new javax.swing.JLabel();
        lblTotalMujeres = new javax.swing.JLabel();
        lblValueTotalMujeres = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        lblValueTotal = new javax.swing.JLabel();
        jPReportes = new javax.swing.JPanel();
        rBtnGraph1 = new javax.swing.JRadioButton();
        rBtnGraph2 = new javax.swing.JRadioButton();
        rBtnGraph3 = new javax.swing.JRadioButton();
        btnGenerarReporte = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gráficos");
        setPreferredSize(new java.awt.Dimension(1400, 800));
        setResizable(false);

        jPGraph1.setPreferredSize(new java.awt.Dimension(1176, 700));

        jspGraf1Mujeres.setBorder(javax.swing.BorderFactory.createTitledBorder("Mujeres"));

        jspGraf1Varones.setBorder(javax.swing.BorderFactory.createTitledBorder("Varones"));

        javax.swing.GroupLayout jPGraph1Layout = new javax.swing.GroupLayout(jPGraph1);
        jPGraph1.setLayout(jPGraph1Layout);
        jPGraph1Layout.setHorizontalGroup(
            jPGraph1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPGraph1Layout.createSequentialGroup()
                .addComponent(jspGraf1Varones, javax.swing.GroupLayout.PREFERRED_SIZE, 543, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspGraf1Mujeres, javax.swing.GroupLayout.DEFAULT_SIZE, 542, Short.MAX_VALUE))
        );
        jPGraph1Layout.setVerticalGroup(
            jPGraph1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPGraph1Layout.createSequentialGroup()
                .addGroup(jPGraph1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jspGraf1Varones, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jspGraf1Mujeres, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 20, Short.MAX_VALUE))
        );

        jTPGraficos.addTab("GRAPH1- Population by Age and Sex", jPGraph1);

        jPGraph2.setPreferredSize(new java.awt.Dimension(1176, 700));

        jspGraf2Mujeres.setBorder(javax.swing.BorderFactory.createTitledBorder("Mujeres"));

        jspGraf2Varones.setBorder(javax.swing.BorderFactory.createTitledBorder("Varones"));

        javax.swing.GroupLayout jPGraph2Layout = new javax.swing.GroupLayout(jPGraph2);
        jPGraph2.setLayout(jPGraph2Layout);
        jPGraph2Layout.setHorizontalGroup(
            jPGraph2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPGraph2Layout.createSequentialGroup()
                .addComponent(jspGraf2Varones, javax.swing.GroupLayout.PREFERRED_SIZE, 543, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspGraf2Mujeres, javax.swing.GroupLayout.DEFAULT_SIZE, 542, Short.MAX_VALUE))
        );
        jPGraph2Layout.setVerticalGroup(
            jPGraph2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPGraph2Layout.createSequentialGroup()
                .addGroup(jPGraph2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jspGraf2Varones, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jspGraf2Mujeres, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 20, Short.MAX_VALUE))
        );

        jTPGraficos.addTab("GRAPH2- Percent of Each Sex", jPGraph2);

        jPGraph3.setPreferredSize(new java.awt.Dimension(1176, 700));

        jspGraf3Mujeres.setBorder(javax.swing.BorderFactory.createTitledBorder("Mujeres"));

        jspGraf3Varones.setBorder(javax.swing.BorderFactory.createTitledBorder("Varones"));

        javax.swing.GroupLayout jPGraph3Layout = new javax.swing.GroupLayout(jPGraph3);
        jPGraph3.setLayout(jPGraph3Layout);
        jPGraph3Layout.setHorizontalGroup(
            jPGraph3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPGraph3Layout.createSequentialGroup()
                .addComponent(jspGraf3Varones, javax.swing.GroupLayout.PREFERRED_SIZE, 543, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspGraf3Mujeres, javax.swing.GroupLayout.DEFAULT_SIZE, 542, Short.MAX_VALUE))
        );
        jPGraph3Layout.setVerticalGroup(
            jPGraph3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPGraph3Layout.createSequentialGroup()
                .addGroup(jPGraph3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jspGraf3Varones, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jspGraf3Mujeres, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 20, Short.MAX_VALUE))
        );

        jTPGraficos.addTab("GRAPH3- Percent of Total Population", jPGraph3);

        jPDatos.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));

        lblTotalVarones.setText("Total Varones:");

        lblValueTotalVarones.setText("0");
        lblValueTotalVarones.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblTotalMujeres.setText("Total Mujeres:");

        lblValueTotalMujeres.setText("0");
        lblValueTotalMujeres.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblTotal.setText("Total:");

        lblValueTotal.setText("0");
        lblValueTotal.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPDatosLayout = new javax.swing.GroupLayout(jPDatos);
        jPDatos.setLayout(jPDatosLayout);
        jPDatosLayout.setHorizontalGroup(
            jPDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPDatosLayout.createSequentialGroup()
                .addComponent(lblTotalVarones)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPDatosLayout.createSequentialGroup()
                .addGroup(jPDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTotalMujeres)
                    .addGroup(jPDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(lblValueTotalMujeres, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblTotal, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblValueTotal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblValueTotalVarones, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPDatosLayout.setVerticalGroup(
            jPDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPDatosLayout.createSequentialGroup()
                .addComponent(lblTotalVarones)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblValueTotalVarones)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblTotalMujeres)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblValueTotalMujeres)
                .addGap(18, 18, 18)
                .addComponent(lblTotal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblValueTotal)
                .addGap(0, 10, Short.MAX_VALUE))
        );

        jPReportes.setBorder(javax.swing.BorderFactory.createTitledBorder("Reportes"));

        btnGRpt.add(rBtnGraph1);
        rBtnGraph1.setText("GRAPH1");

        btnGRpt.add(rBtnGraph2);
        rBtnGraph2.setText("GRAPH2");

        btnGRpt.add(rBtnGraph3);
        rBtnGraph3.setText("GRAPH3");

        btnGenerarReporte.setText("Generar");
        btnGenerarReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGenerarReporteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPReportesLayout = new javax.swing.GroupLayout(jPReportes);
        jPReportes.setLayout(jPReportesLayout);
        jPReportesLayout.setHorizontalGroup(
            jPReportesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPReportesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPReportesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rBtnGraph1)
                    .addComponent(rBtnGraph2)
                    .addComponent(rBtnGraph3)
                    .addComponent(btnGenerarReporte, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        jPReportesLayout.setVerticalGroup(
            jPReportesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPReportesLayout.createSequentialGroup()
                .addComponent(rBtnGraph1)
                .addGap(18, 18, 18)
                .addComponent(rBtnGraph2)
                .addGap(18, 18, 18)
                .addComponent(rBtnGraph3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addComponent(btnGenerarReporte))
        );

        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTPGraficos, javax.swing.GroupLayout.PREFERRED_SIZE, 1096, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPDatos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPReportes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(btnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jPDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPReportes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCerrar))
                    .addComponent(jTPGraficos, javax.swing.GroupLayout.PREFERRED_SIZE, 748, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 37, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGenerarReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenerarReporteActionPerformed
        opcionGenerarReporte();
    }//GEN-LAST:event_btnGenerarReporteActionPerformed

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void opcionGenerarReporte() {
        UtilLayoutPantalla.initWaiting(this);
        try {
            if (rBtnGraph1.isSelected()) {
                gestor.generarReporteGraph1();
                return;
            }
            if (rBtnGraph2.isSelected()) {
                gestor.generarReporteGraph2();
                return;
            }
            if (rBtnGraph3.isSelected()) {
                gestor.generarReporteGraph3();
            }
        } catch (ProcessReportException ex) {
            UtilLayoutPantalla.endWaiting(this);
            JOptionPane.showMessageDialog(null, "Error en aplicación para generación de reporte: " + ex.getMessage(), "Mensaje", JOptionPane.ERROR_MESSAGE);
        } catch (JRException ex) {
            UtilLayoutPantalla.endWaiting(this);
            JOptionPane.showMessageDialog(null, "Error en biblioteca para generación de reporte: " + ex.getMessage(), "Mensaje", JOptionPane.ERROR_MESSAGE);
        } catch (Exception ex) {
            UtilLayoutPantalla.endWaiting(this);
            JOptionPane.showMessageDialog(null, "Error al generar reporte: " + ex.getMessage(), "Mensaje", JOptionPane.ERROR_MESSAGE);
        } finally {
            UtilLayoutPantalla.endWaiting(this);
        }
    }

    public void abrirPantalla() {
        try {
            mostrarGraficoGraph1();
            mostrarGraficoGraph2();
            mostrarGraficoGraph3();
            setDatos();
            UtilLayoutPantalla.centrarEnPantallaJFrame(this);
            UtilLayoutPantalla.insertarIcono(this, ConstantsModuloPpal.PATH_ICONO_PAS);
            setVisible(true);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error al intentar mostrar gráficos: " + ex.getMessage(), "Mensaje", JOptionPane.ERROR_MESSAGE);
            this.dispose();
        }

    }

    private void mostrarGraficoGraph1() throws Exception {
        try {
            JFreeChart chartVarones = gestor.getChartGraph1Varones();
            JFreeChart chartMujeres = gestor.getChartGraph1Mujeres();

            mostrarGraficoEnPanel(jspGraf1Varones, chartVarones);
            mostrarGraficoEnPanel(jspGraf1Mujeres, chartMujeres);

        } catch (Exception ex) {
            throw ex;
        }

    }

    private void mostrarGraficoGraph2() throws Exception {
        try {
            JFreeChart chartVarones = gestor.getChartGraph2Varones();
            JFreeChart chartMujeres = gestor.getChartGraph2Mujeres();

            mostrarGraficoEnPanel(jspGraf2Varones, chartVarones);
            mostrarGraficoEnPanel(jspGraf2Mujeres, chartMujeres);

        } catch (Exception ex) {
            throw ex;
        }

    }

    private void mostrarGraficoGraph3() throws Exception {
        try {
            JFreeChart chartVarones = gestor.getChartGraph3Varones();
            JFreeChart chartMujeres = gestor.getChartGraph3Mujeres();

            mostrarGraficoEnPanel(jspGraf3Varones, chartVarones);
            mostrarGraficoEnPanel(jspGraf3Mujeres, chartMujeres);

        } catch (Exception ex) {
            throw ex;
        }

    }

    private void mostrarGraficoEnPanel(JScrollPane jspGraf, JFreeChart chart) {
        JPanel panel = new ChartPanel(chart); //creo el CahrtPanel con la grafica ya creada 
        panel.setPreferredSize(new Dimension(Constants.PLLA_PANELCHAR_WIDTH, Constants.PLLA_PANELCHAR_HEIGHT));
        jspGraf.setViewportView(panel);
        panel.setVisible(true); // hago Visible el ChartPanel -- sin esta linea, anda igual. 
        panel.repaint(); // redibujo el ChartPanel 
        jspGraf.repaint(); // redibujo el JScrollPane

    }

    private void setDatos() {
        String subtitulo = gestor.getDataRptChart().getSubTitulo();
        if (subtitulo != Constants.SUBTITULO_VALUE_NULL) {
            subtitulo = "- " + subtitulo;
        } else {
            subtitulo = " ";
        }

        this.setTitle(gestor.getDataRptChart().getTitulo() + subtitulo);
        this.lblValueTotalVarones.setText(String.valueOf(gestor.getDataRptChart().getTotalVarones()));
        this.lblValueTotalMujeres.setText(String.valueOf(gestor.getDataRptChart().getTotalMujeres()));
        this.lblValueTotal.setText(String.valueOf(gestor.getDataRptChart().getTotal()));
        this.rBtnGraph1.setSelected(true);
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.ButtonGroup btnGRpt;
    private javax.swing.JButton btnGenerarReporte;
    private javax.swing.JPanel jPDatos;
    private javax.swing.JPanel jPGraph1;
    private javax.swing.JPanel jPGraph2;
    private javax.swing.JPanel jPGraph3;
    private javax.swing.JPanel jPReportes;
    private javax.swing.JTabbedPane jTPGraficos;
    private javax.swing.JScrollPane jspGraf1Mujeres;
    private javax.swing.JScrollPane jspGraf1Varones;
    private javax.swing.JScrollPane jspGraf2Mujeres;
    private javax.swing.JScrollPane jspGraf2Varones;
    private javax.swing.JScrollPane jspGraf3Mujeres;
    private javax.swing.JScrollPane jspGraf3Varones;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JLabel lblTotalMujeres;
    private javax.swing.JLabel lblTotalVarones;
    private javax.swing.JLabel lblValueTotal;
    private javax.swing.JLabel lblValueTotalMujeres;
    private javax.swing.JLabel lblValueTotalVarones;
    private javax.swing.JRadioButton rBtnGraph1;
    private javax.swing.JRadioButton rBtnGraph2;
    private javax.swing.JRadioButton rBtnGraph3;
    // End of variables declaration//GEN-END:variables
}
